import socket


def send_msg(udp_socket):
    dest_ip = input("请输入对方的IP:")
    dest_port = int(input("请输入对方的port:"))
    send_data = input("请输入要输入的内容:")
    udp_socket.sendto(send_data.encode("utf-8"), (dest_ip, dest_port))


def resc_msg(udp_socket):
    # 接受并显示
    resc_data = udp_socket.recvfrom(1024)
    print("%s:%s" % (str(resc_data[1]), resc_data[0].decode("gbk")))


def main():
    # 创建套接字
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # 绑定信息
    udp_socket.bind(("", 51550))

    while True:
        print("....xx聊天器.....")
        print("1:发送消息")
        print("2:接受消息")
        print("3:退出系统")
        op = input("请输入功能input:")
        if op == "1":
            send_msg(udp_socket)
        elif op == "2":
            resc_msg(udp_socket)
        elif op == "0":
            break
        else:
            print("输入有误,请重新输入....")
    # udp_socket.close()


if __name__ == "__main__":
    main()
